home *** CD-ROM | disk | FTP | other *** search
/ QuickTime - The Beta Release / QuickTime - The Beta Release.iso / Programming Stuff / vdig sample / DIGIUtils.c next >
Text File  |  1991-09-05  |  11KB  |  398 lines

  1. /*
  2.     File:        DIGIUtils.c
  3. */
  4.  
  5. /* Digitizer Utilities DIGIUtils.c */
  6.  
  7. #include <QuickDraw.h>
  8.  
  9. #include "Components.h"
  10. #include "DIGIUtils.h"
  11. #include "video digitizer.h"
  12. /*#include "moonraker.h"*/
  13. #include "RO364.h"
  14. /* #include "TSdigi.h" */
  15. #include "Matrix.h"
  16. #include "errHandler.h"
  17.  
  18. pascal long TestBuffers(void);
  19.  
  20. #define        WITHMATRIX            /* define it if you want to use the simplest interface to the digitizer */
  21.  
  22. extern    ComponentInstance        gti;
  23. extern    Component                gt;
  24. extern Boolean            ghFlipState, gvFlipState;
  25.  
  26. pascal void ValidatePixMap(PixMapHandle *pm, Rect *r, short *tx, short *ty)
  27. {
  28. Rect            tmpDstRect;                /* temp that holds the incoming rect */    
  29. Rect            iRect;                    /* holds the intersect rect on the gdevice */
  30. PixMap            screenPixMap;            /* ultimately holds the pix map to be returned */
  31. PixMapHandle    tmpDstPixMapHandle;        
  32. short            w;                        /* incoming rect width */
  33. short            h;                        /* incoming rect height */
  34. Boolean            allOn;                    /* true if the rect is completely contained on the gdevice */
  35. Boolean            crossScreens;            /* true if the rect crosses screens */
  36. Point            tmpPoint;
  37. GDHandle        gdh;
  38. short            top;
  39. short            left;
  40. Rect            aRect;
  41.  
  42.     tmpDstRect = *r;
  43.     tmpDstPixMapHandle = *pm;
  44.     
  45.     screenPixMap = **tmpDstPixMapHandle;
  46.     w = r->right - r->left;
  47.     h = r->bottom - r->top;
  48.     
  49.     allOn = FALSE;
  50.     crossScreens = FALSE;
  51.     
  52.     if ( (*tmpDstPixMapHandle)->baseAddr == (*(Ptr *) 0x824) ) {
  53.         top = (*tmpDstPixMapHandle)->bounds.top;
  54.         left = (*tmpDstPixMapHandle)->bounds.left;
  55.         RectLocalToGlobal(&tmpDstRect);
  56.         SetPt(&tmpPoint, tmpDstRect.left, tmpDstRect.top);
  57.         gdh = GetDeviceList();
  58.         while ((gdh) != 0L && (PtInRect(tmpPoint, &(*gdh)->gdRect) != true) ) {
  59.             gdh = GetNextDevice(gdh);
  60.         }
  61.         
  62.         SectRect(&tmpDstRect, &(*gdh)->gdRect, &iRect);
  63.         if (EqualRect(&tmpDstRect, &iRect)) allOn = TRUE;
  64.  
  65.         screenPixMap = **(*gdh)->gdPMap;
  66.         
  67.         ***pm = screenPixMap;
  68.         aRect = *r;
  69.         RectLocalToGlobal(&aRect);
  70.         
  71.         *tx = aRect.left;
  72.         *ty = aRect.top;
  73.         
  74.     }
  75. }
  76.         
  77.  
  78. pascal void CalculateScale(Rect *r1, Rect *r2, Fixed *sx, Fixed *sy)
  79. /* This routine takes two rects and calculates the scaling coefficients Sx and Sy.
  80.    The order is to scale r1 by Sx and Sy to get r2.
  81.  */
  82. {
  83.     short    h1,w1,h2,w2;
  84.  
  85.     h1 = r1->bottom - r1->top;
  86.     w1 = r1->right - r1->left;
  87.     h2 = r2->bottom - r2->top;
  88.     w2 = r2->right - r2->left;
  89.     
  90.     *sx = FixRatio(w2,w1);
  91.     *sy = FixRatio(h2,h1);
  92.  
  93. }
  94.     
  95. pascal WindowPtr GetNewVWindow(short windowID, Ptr wStorage, WindowPtr behind)
  96. {
  97. Str255 defType        = "\pvdig";
  98. Str255 defSubType    = "\props";
  99. Str255 defManu        = "\props";
  100.     ComponentDescription foo;
  101.     long            result = 0;
  102.     Rect            r;
  103.     Rect            activeRect;
  104.     
  105.     WindowPtr        vidWindow;
  106.     long             aaa, bbb;
  107.     
  108.     vidWindow = GetNewCWindow(windowID,wStorage,behind);    /* Make a new window */
  109.  
  110. /* First find a digitizer thing and open it */          
  111.           foo.componentType = videoDigitizerComponentType;
  112.           foo.componentSubType = 0;
  113.           foo.componentManufacturer = 0;
  114.           foo.componentFlags = 0;
  115.           foo.componentFlagsMask = 0;
  116. /*Use this one if the thing is built into project*/
  117.           gt = RegisterComponent(&foo, (void *)RO364Thing, 1, 0, 0, 0);   
  118. /* Use this one if using a real thing *//**/
  119. /*          gt = FindNextComponent((Component)0x0L,&foo);              */
  120.  
  121.           gti = OpenComponent(gt); 
  122.  
  123. /* Initialize the digitizer to full screen and digitizing off */
  124.           result = VDGetMaxSrcRect(gti,ntscIn,&r);
  125.           result = VDGetActiveSrcRect(gti,ntscIn,&activeRect);
  126.           result = VDSetDigitizerRect(gti,&activeRect);
  127.           result = VDSetPlayThruOnOff(gti,vdPlayThruOff); 
  128.         if (result == 0) result = VDGetCurrentFlags(gti,&aaa, &bbb);            
  129.  
  130. /* Do what it takes to change the video digitizer */
  131.         result = UpdateVDDestination(vidWindow);
  132.           
  133.           if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOn);            
  134.           
  135. /* Throw up an alert if there was a problem */
  136.         if (result)ErrAlert(result,(char *)"\pNewVideoWindow");        
  137.         return (vidWindow);
  138. }
  139. pascal void DragVideoWindow(WindowPtr theWindow, Point startPt, Rect *dragRect)
  140. {
  141.     short    tx;
  142.     short    ty;
  143.     long    result = 0;
  144.     
  145.     Rect    r;
  146.     MatrixRecord    aMatrix;
  147.     DigitizerInfo    digiInfo;
  148.     
  149.     Fixed    ftx, fty, sx, sy;
  150.         
  151.     PixMapHandle aPixMapHandle;
  152.     long             aaa, bbb;
  153.     PicHandle    picH;
  154.     short        qderr;          
  155.     GDHandle    savegdh;
  156.     
  157.     
  158. /* Freeze video so drag update will work right and do the inherited DragWindow */
  159.           if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOff);        
  160.           DragWindow(theWindow, startPt, dragRect);
  161.  
  162. /* Do what it takes to change the video digitizer */
  163.     result = UpdateVDDestination(theWindow);
  164.  
  165. /* Turn on the digitizer at the new location if all is well */
  166.           if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOn);            
  167.  
  168. /* Test the VDSetupBuffers stuff */
  169.  
  170.         if (result == 0) result = TestBuffers(); 
  171.  
  172. /* Throw up an alert if there was a problem */
  173.         if (result)ErrAlert(result,(char *)"\pDragVideoWindow");        
  174. }
  175.  
  176. pascal void GrowVideoWindow(WindowPtr theWindow, Point startPt)
  177. {
  178. long    result = 0;
  179. long    theResult;
  180. Rect    r;
  181.  
  182. /* Freeze video so drag update will work right and do the inherited GrowWindow (with dragRect limited to MaxSrcRect) */
  183.     if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOff);        
  184.     result = VDGetMaxSrcRect(gti,ntscIn,&r);
  185.     theResult = GrowWindow( theWindow, startPt, &r );
  186.  
  187. /* If there was no resizing operation, just exit and turn the digitizer back on (now this will work like a frame grab */
  188.     if (theResult == 0) {
  189.           if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOff);            
  190.         return;
  191.     }
  192.  
  193. /* Do the inherited SizeWindow after GrowWindow complete */
  194.     SizeWindow( theWindow, LoWord(theResult), HiWord(theResult), 1);
  195. /*    InvalRect(&theWindow->portRect); */
  196.  
  197. /* Do what it takes to change the video digitizer */
  198.     result = UpdateVDDestination(theWindow);
  199.  
  200. /* Turn on the digitizer with the new size if all is well */
  201.           if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOn);            
  202.  
  203. /* Throw up an alert if there was a problem */
  204.     if (result)ErrAlert(result,(char *)"\pGrowVideoWindow");        
  205. }
  206.  
  207. pascal void DisposeVideoWindow(WindowPtr theWindow)
  208. {
  209. long    result = 0;
  210.  
  211.     result = VDSetPlayThruOnOff(gti,vdPlayThruOff);    /* Freeze video so drag update will work right */        
  212.     DisposeWindow(theWindow);
  213.     CloseComponent(gti);
  214. }
  215.  
  216. pascal void MoveVideoWindow(WindowPtr theWindow, short h, short v, Boolean front)
  217. {
  218.     long    result = 0;
  219.         
  220. /* Freeze video so drag update will work right and do the inherited MoveWindow */
  221.     result = VDSetPlayThruOnOff(gti,vdPlayThruOff);            
  222.     MoveWindow(theWindow,h,v,front);
  223.  
  224. /* Do what it takes to change the video digitizer */
  225.     result = UpdateVDDestination(theWindow);
  226.  
  227. /* Turn on the digitizer with the new location if all is well */
  228.     if (result == 0) result = VDSetPlayThruOnOff(gti,vdPlayThruOn);            
  229.  
  230. /* Throw up an alert if there was a problem */
  231.     if (result)ErrAlert(result,(char *)"\pMoveVideoWindow");        
  232. }
  233.  
  234. pascal long    UpdateVDDestination( WindowPtr theWindow )
  235. {
  236.     short    tx;
  237.     short    ty;
  238.     long    result = 0;
  239.     
  240.     Rect    r;
  241.     Rect    r1;
  242.     Rect    aRect;
  243.     MatrixRecord    aMatrix;
  244.     DigitizerInfo    digiInfo;
  245.     
  246.     Fixed    ftx, fty, sx, sy;
  247.         
  248.     PixMapHandle aPixMapHandle;              
  249.  
  250. /* Allocate a temporary pixmap and preflight it (getting a new pixmap, tx, and ty) */
  251.         aPixMapHandle = (PixMapHandle) NewHandle( sizeof(PixMap) ); 
  252.         **aPixMapHandle = **((CWindowPtr)theWindow)->portPixMap;
  253.         ValidatePixMap(&aPixMapHandle, &theWindow->portRect, &tx, &ty);
  254.  
  255. #ifdef WITHMATRIX
  256.     /* Convert translation coefficients to fixed */        
  257.         ftx = FixRatio(tx,1);
  258.         fty = FixRatio(ty,1);
  259.  
  260.     /* Calculate the scaling coefficients and set up the transformation matrix (scale and translate */
  261.         if (result == 0) result = VDGetDigitizerRect(gti,&r);
  262.         RectMatrix(&aMatrix,&r,&(*theWindow).portRect);
  263.         if (ghFlipState) aMatrix.matrix[0][0] = -aMatrix.matrix[0][0];
  264.         if (gvFlipState) aMatrix.matrix[1][1] = -aMatrix.matrix[1][1];    
  265.         TranslateMatrixTo( &aMatrix, ftx, fty);
  266.  
  267.     /* Set the digitizers destination */            
  268.         if (result == 0) result = VDSetPlayThruDestination(gti, aPixMapHandle, &theWindow->portRect, &aMatrix, 0L);
  269.  
  270. #else
  271.           
  272.     /* A simpler interface for playthrough might be (play thru state on/off is retained in the call) */
  273.         aRect = theWindow->portRect;
  274.         RectLocalToGlobal(&aRect);
  275.         if (result == 0) result = VDSetPlayThruDestination(gti, aPixMapHandle, &aRect, 0L, 0L);
  276. /*        if (result == 0) result = VDSetPlayThruDestination(gti, aPixMapHandle, &r1, 0L, 0L);*/    /* Use this to inset the video */
  277.  
  278. #endif
  279.         DisposHandle((Handle)aPixMapHandle);
  280.  
  281. /* Throw up an alert if there was a problem */
  282.     if (result)ErrAlert(result,(char *)"\pUpdateVDDestination");        
  283.  
  284.     return (result);
  285. }
  286.  
  287. pascal long TestBuffers() {
  288.  
  289. VdigBufferRec    b0, b1, b2;
  290. VdigBufferRecListHandle h;
  291. Ptr        p;
  292. Handle    storage;
  293. long    retstat;
  294. Rect    r;
  295. short    i;
  296.     
  297.     p = NewPtr (3 * sizeof(VdigBufferRec));
  298.     h = (VdigBufferRecListHandle) NewHandle (sizeof(VdigBufferRecList) + 
  299.                                              3 * sizeof(VdigBufferRec));
  300.     
  301.     retstat = VDGetMaxAuxBuffer(gti, &b0.dest, &r);  
  302. /* Initialize 3 buffers */
  303. /*    b0.nextDest = NewPixMap(); 
  304.     (**b0.nextDest).baseAddr = (Ptr)0xF9900000;
  305. */
  306.     retstat = VDGetMaxAuxBuffer(gti, &b0.dest, &r);  
  307.     SetPt (&b0.location, -200, 0);
  308.     b0.reserved = 0;
  309.  
  310. /*    b1.nextDest = NewPixMap();
  311.     (**b1.nextDest).baseAddr = (Ptr)0xFaa00000;
  312. */
  313.     retstat = VDGetMaxAuxBuffer(gti, &b1.dest, &r);  
  314.     SetPt (&b1.location, -400, 0);
  315.     b1.reserved = 0;
  316.  
  317. /*    b2.nextDest = NewPixMap();
  318.     (**b2.nextDest).baseAddr = (Ptr)0xFbb00000;
  319. */
  320.     retstat = VDGetMaxAuxBuffer(gti, &b2.dest, &r);  
  321.     SetPt (&b2.location, -600, 0);
  322.     b2.reserved = 0;
  323.  
  324. /* Copy to the real buffer list pointer */
  325.     (**h).count = 3;
  326.     BlockMove(&b0, (**h).list, sizeof(VdigBufferRec));
  327.     BlockMove(&b1, (**h).list + 1, sizeof(VdigBufferRec));
  328.     BlockMove(&b2, (**h).list + 2, sizeof(VdigBufferRec));    
  329.  
  330. /* An alternate way ... of doing it */
  331. /*    BlockMove(&b0, p, sizeof(VdigBufferRec));
  332.     BlockMove(&b1, p + sizeof(VdigBufferRec), sizeof(VdigBufferRec));
  333.     BlockMove(&b2, p + 2*sizeof(VdigBufferRec), sizeof(VdigBufferRec));
  334.  
  335.     (**h).list[0] = *(VdigBufferRec *)p;
  336.     (**h).list[1] = *(VdigBufferRec *)(p + sizeof(VdigBufferRec));
  337.     (**h).list[2] = *(VdigBufferRec *)(p + 2*sizeof(VdigBufferRec));
  338. */
  339.  
  340. /* Simulate the new call */
  341.     retstat = VDSetupBuffers(gti, h);
  342.  
  343.     for (i=0; i<30; i++) {
  344.         retstat = VDGrabOneFrameAsync2(gti,1);
  345.         while (!VDDone2(gti,0)); 
  346.         retstat = VDGrabOneFrameAsync2(gti,2);
  347.         while (!VDDone2(gti,1)); 
  348.         retstat = VDGrabOneFrameAsync2(gti,0);
  349.         while (!VDDone2(gti,2)); 
  350.     } 
  351.     
  352.             
  353.     DisposPtr(p);
  354.     DisposHandle((Handle)h);
  355. /*    DisposPixMap(b0.nextDest);
  356.     DisposPixMap(b1.nextDest);
  357.     DisposPixMap(b2.nextDest);
  358. */
  359. }
  360. /* ======================================================= */
  361.  
  362. void RectLocalToGlobal(Rect *r)
  363. {
  364.     Point pt;
  365.  
  366.     pt.v = r->top;
  367.     pt.h = r->left;
  368.     LocalToGlobal(&pt);
  369.     r->top = pt.v;
  370.     r->left = pt.h;
  371.  
  372.     pt.v = r->bottom;
  373.     pt.h = r->right;
  374.     LocalToGlobal(&pt);
  375.     r->bottom = pt.v;
  376.     r->right = pt.h;
  377. }
  378.  
  379. /* ======================================================= */
  380.  
  381. void RectGlobalToLocal(Rect *r)
  382. {
  383.     Point pt;
  384.  
  385.     pt.v = r->top;
  386.     pt.h = r->left;
  387.     GlobalToLocal(&pt);
  388.     r->top = pt.v;
  389.     r->left = pt.h;
  390.  
  391.     pt.v = r->bottom;
  392.     pt.h = r->right;
  393.     GlobalToLocal(&pt);
  394.     r->bottom = pt.v;
  395.     r->right = pt.h;
  396. }
  397.  
  398.